package com.ckg.auto.impl;

import com.ckg.auto.LoggerService;
import com.ckg.auto.UserRepository;

// 用户服务（使用自动装配）
public class UserService {
    // 按类型装配（要求容器中只有一个 UserRepository）
    private UserRepository primaryRepository;

    // 按名称装配（指定 bean 名称）
    private LoggerService loggerService;

    // 简单类型（需手动配置）
    private int maxRetry;

    // Setter 方法必须保留
    public void setPrimaryRepository(UserRepository repository) {
        this.primaryRepository = repository;
    }

    public void setLoggerService(LoggerService loggerService) {
        this.loggerService = loggerService;
    }

    public void setMaxRetry(int maxRetry) {
        this.maxRetry = maxRetry;
    }

    public void registerUser(String username) {
        loggerService.log("开始注册用户: " + username);

        try {
            primaryRepository.saveUser(username);
            loggerService.log("用户注册成功: " + username);
        } catch (Exception e) {
            loggerService.log("用户注册失败: " + username);
            System.out.println("🔄 重试中 (" + maxRetry + "次剩余)");
        }
    }
}
